package com.flowerplus.store.common.data.utils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by z on.
 * Des:时间转换器工具类
 * Date: 2017/3/17
 */

public class DateUtil {


    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return
     */
    public static Long dateToStamp(String s) {
        long res = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = ts;
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return res;
    }

    /**
     * 将时间戳转换为时间
     *
     * @param s
     * @return
     */
    public static String stampToDate(Long s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
        Date date = new Date(s);
        String res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为自定义格式时间
     * @param s
     * @param format
     * @return
     */
    public static String stampToDate(Long s, String format) {
        if (s==null) {
            return  "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(s);
        String res = simpleDateFormat.format(date);
        return res;
    }
    public static String stampToDate(Date date, String format) {
        if (date==null) {
            return  "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String res = simpleDateFormat.format(date);
        return res;
    }
}
